<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryUser">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="产品名称" width="180" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="英文名称" width="180" sortable>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template scope="scope">
          <el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--分页栏那一栏-->
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size=pageSize
                     :current-page=currentPage :total="totals" style="float:right;">
      </el-pagination>
    </el-col>


    <el-dialog :title="titleUpdate" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="产品品牌" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="英文名称" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>

export default {
  data() {
    return {
      //1-准备分页数据
      users: [],//数据源
      totals: 0,//总条数
      currentPage: 1,//当前页面
      pageSize: 5,//页面条数
      //2-高级查询
      keyword: "",//关键字
      //3-批量删除
      sels: [],//列表选中列
      //4-新增和删除的标题
      titleUpdate: "",
      //4.1-编辑和新增界面是否显示
      saveFormVisible: false,
      //4.2-新增和编辑对话框界面数据
      saveForm: {
        id: null,
        name: '',
        sn: ""
      },
      //4.3-修改规则
      saveFormRules: {
        name: [
          {required: true, message: '请输入产品名称', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入英文名称', trigger: 'blur'}
        ],
      },
      //4.4等待框
      saveLoading: false,
      //4.5新增页面显示
      addFormVisible: false,
      //4.6新增界面数据
      addForm: {
        id: null,
        name: '',
        sn: ""
      },
      //4.7下拉框模型的值
      department: [],

      filters: {
        name: ''
      },


      listLoading: false,
    }
  },
  methods: {
    //1-获取用户列表
    getUsers() {
      let Pages = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword,
      }
      //打开等待圈
      this.listLoading = true;
      //获取后端的当前页对象
      this.$http.post("/systemDictionaryType/", Pages).then(result => {
        this.users = result.data.data;
        this.totals = result.data.totals;
        this.listLoading = false;
      }).catch(result => {
        this.$message.error("")
      })
    },
    //2-获取分页数据,然后再进行查询
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getUsers();
    },
    //3-高级查询
    queryUser() {
      this.currentPage = 1;
      this.getUsers();
    },

    //4-删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/systemDictionaryType/" + row.id).then(result => {
          //成功之后的处理
          if (result.data.success) {
            //计算总页数 = 最后一页
            let totalPage = Math.ceil(this.totals / this.pageSize);
            //如果当前页是最后一页(总页数)，而且是最后一条数据，并且不是第一页，让当前页=当前页-1
            if (this.currentPage == totalPage
                && (this.totals - 1) % this.pageSize == 0 && this.currentPage != 1) {
              this.currentPage = this.currentPage - 1;
            }
            this.getUsers();
          } else {
            this.$message.error("服务器繁忙,500错误");
          }
          this.listLoading = false;
        }).catch(result => {
          this.$message.error("服务器繁忙,400-404错误")
        }).catch(() => {
          //取消窗口
        });
      });
    },
    //5-批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);//
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/systemDictionaryType/", ids).then((result) => {
          if (result.data.success) {
            this.currentPage = 1;
            this.getUsers();
          } else {
            this.$message.error("系统繁忙[500]")
          }
          this.listLoading = false;
        }).catch((result) => {
          this.$message.error("系统繁忙[400]-[404]")
        })
      }).catch(() => {

      });
    },
    //6.1显示编辑界面
    handleSave: function (index, row) {
      this.getdepartment();//下拉模态框数据
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);//将里面的表单所有数据都给他
    },
    //6.2-修改与保存按钮
    saveSubmit: function () {
      //表单验证,如果name和电话为空,就不能提交
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            this.$http.put("/systemDictionaryType", para).then((result => {
              if (result.data.success) {
                this.getUsers();
                this.saveFormVisible = false;
              } else {
                this.$message.error("服务器繁忙500错误")
              }
              this.saveLoading = false;
            })).catch((result => {
              this.$message.error("服务器繁忙 400-404错误")
            }));
          });
        }
      });
    },
    //7-新增操作
    handleAdd: function () {
      this.getdepartment();//下拉模态框数据
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        sn: '',
        name: ""
      };
    },
    //7.1-方法中新增下拉选框
    getdepartment() {
      this.$http.get("/systemDictionaryType").then((result => {
        this.department = result.data;
      })).catch((resutl) => {

      })

    },


    selsChange: function (sels) {
      this.sels = sels;
    },

  },
  mounted() {
    this.getUsers();
  }
}

</script>

<style scoped>

</style>